package com.yxhl.zoume.core.func.pay.presenter;

import android.content.Context;

import com.yxhl.zoume.common.presenter.BasePresenter;
import com.yxhl.zoume.common.ui.view.BaseView;
import com.yxhl.zoume.core.func.pay.view.WalletPayView;
import com.yxhl.zoume.data.http.rest.param.pay.WalletPayParam;
import com.yxhl.zoume.data.http.rest.response.pay.WalletPayResponse;
import com.yxhl.zoume.domain.interactor.ZMSubscriber;
import com.yxhl.zoume.domain.interactor.pay.WalletPayUseCase;
import com.yxhl.zoume.utils.RSA;

import javax.inject.Inject;

/**
 * Created by vhawk on 2016/11/15.
 */

public class WalletPayPresenter extends BasePresenter {

    @Inject
    WalletPayUseCase useCase;

    private WalletPayView view;

    @Inject
    public WalletPayPresenter(Context context) {
        super(context);
    }

    @Override
    public void attachView(BaseView view) {
        super.attachView(view);
        this.view = (WalletPayView) view;
    }

    public void payByWallet(String orderId) {
        payByWallet(orderId, null, null);
    }


    public void payByWallet(String orderId, String couponId, String discountFee) {
        WalletPayParam payParam = new WalletPayParam();

        payParam.setId(RSA.encryptWithRSA(orderId))
                .setCouponUserRelId(couponId)
                .setDisCountFee(discountFee)
//                .setBase64EncodingStr(RSA.encryptWithRSA(orderId))
                .setMobile(getMobile())
                .setToken(getToken());

        useCase.execute(payParam,new ZMSubscriber<WalletPayResponse>() {
                    @Override
                    public void onNext(WalletPayResponse walletPayResponse) {
                        super.onNext(walletPayResponse);
                        if (walletPayResponse.isSucc()) {
                            view.handleWalletPaySucceed();
                        } else {
                            view.handleWalletPayFailure(walletPayResponse.getResultMessage());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        view.handleWalletPayError();
                    }
                });
    }
}
